function activeEquipmentPage(){
	$('#editSurgery').hide();
	$('#surgeryTeamTemplateDialog').remove();
	$('#equipmentTemplate').remove();
	var recordId = $('#recordId').val();	
	getSurgeryEquipment(recordId);
}

function actionBackSurgeryPage(){
	/*$('#equipmentTemplate').remove();
	$('#surgery_equipment_div').hide();
	$('#editSurgery').show('slow');*/
	
	$('#equipmentTemplate').remove();
	var recordId = $('#recordId').val();
	loadOpenSurgery(recordId, '');

}

function showEquipmentTemplateDialog() {	
	$('#equipmentTemplate').dialog('open');
}

function registerAutoComplete(tableName, medicine) {
	if (medicine == 1) {
		$('#' + tableName + ' input.auto_complete').autocomplete('../SurgeryEquipment/searchMedicine?set_ajax_view',
												{
													multiple: false,
													matchContains: true,
													autoFill: true,
													cacheLength: 1,
													formatItem: function(data, i, total) {		
														return data[0];
													}
												});
	} else if (medicine == 2) {
		$('#' + tableName + ' input.auto_complete').autocomplete('../SurgeryEquipment/searchEquipment?set_ajax_view',
														{
															multiple: false,
															matchContains: true,
															autoFill: true,
															formatItem: function(data, i, total) {		
																return data[0];
															}
														});	
	}	
																									
}

function getTemplate(templateId) {
	$.ajax({
		type: 'GET',
		url: '../SurgeryEquipment/GetTemplate?set_ajax_view',
		data: 'templateId=' + templateId,		
		beforeSend: function() {
			$('#popUpDialog').hide();
		},
		success: function(data) {
			$('#equipmentTemplateDetails').html(data);
			registerAutoComplete('templateMedicineTbl', 1);
			registerAutoComplete('templateEquipmentTbl', 2);
			$('#popUpDialog').show();
		}
	});
}	

function pickTemplate(templateId) {	
	$.ajax({
		type: 	'GET',
		url: 	'../SurgeryEquipment/PickupTemplate?set_ajax_view',
		data: 	'templateId=' + templateId,
		dataType: 'json',
		beforeSend: function() {
			var img = "<img id='updatingTemplate' style='float: left; margin-left: 10px;' src='../images/ajax-loader.gif' />";
			$('.ui-dialog-buttonpane').prepend(img);			
		},
		success: function(data) {
			$('.ui-dialog-buttonpane img:first-child').remove();
			$('#equipmentTemplate').dialog('close');
			
			$('#medicineTbl tbody').html(data.medicineBody);
			$('#equipmentTbl tbody').html(data.equipmentBody);
		}
	});
}
	
function getEquipmentInfo(medicine, Id){
	var equipment;
	// One <tr> for <thead>
	var medicine_size = $('#medicineTbl tr').length - 2;
	var equipment_size = $('#equipmentTbl tr').length - 2;
		
	if(medicine == 'yes'){
		equipment = 'medicine';
		if(Id == medicine_size ){
			var new_index = medicine_size + 1;
			var new_row = " <tr><input id='medicine_id_" + new_index + "' name='medicine_id_" + new_index + "' type='hidden' value=''>"  
							  + "<td><span id='medicine_type_span_"+ new_index +"' name='medicine_type_span_"+ new_index +"'></span></td>"
							  + "<td><input type='text' id='medicine_name_"+ new_index + "' name='medicine_name_"+ new_index + "' value='' onchange=\"javascript:getEquipmentInfo('yes',"+ new_index +");\"></td>"
							  + "<td><input type='text' id='medicine_quantity_"+ new_index + "' name='medicine_quantity_"+ new_index + "' size='4' value=''>"
							  + "&nbsp;<input type='text' id='medicine_quantity_title_"+ new_index + "' name='medicine_quantity_title_"+ new_index + "' size='6' value=''></td>"
							  + "</tr>" ;
			$('#medicineTbl tbody').append(new_row);			
			$('#medicine_name_' + new_index).autocomplete('../SurgeryEquipment/searchMedicine?set_ajax_view',
														{
															multiple: false,
															matchContains: true,
															autoFill: true,
															cacheLength: 1,
															formatItem: function(data, i, total) {		
																return data[0];
															}
														});
		}
	}else{
		equipment = 'equipment';
		if(Id == equipment_size ){
			var new_index = equipment_size + 1;
			var new_row = " <tr><input id='equipment_id_" + new_index + "' name='equipment_id_" + new_index + "' type='hidden' value=''>"
							  + "<td><span id='equipment_type_span_"+ new_index +"' name='equipment_type_span_"+ new_index +"'></span></td>"  
							  + "<td><input type='text' id='equipment_name_"+ new_index + "' name='equipment_name_"+ new_index + "' value='' onchange=\"javascript:getEquipmentInfo('no',"+ new_index +");\"></td>"
							  + "<td><input type='text' id='equipment_quantity_"+ new_index + "' name='equipment_quantity_"+ new_index + "' size='4' value=''>"
							  + "&nbsp;<input type='text' id='equipment_quantity_title_"+ new_index + "' name='equipment_quantity_title_"+ new_index + "' size='6' value=''></td>"
							  + "</tr>" ;
			$('#equipmentTbl tbody').append(new_row);			
			
			$('#equipment_name_' + new_index).autocomplete('../SurgeryEquipment/searchEquipment?set_ajax_view',
														{
															multiple: false,
															matchContains: true,
															autoFill: true,
															cacheLength: 1,
															formatItem: function(data, i, total) {		
																return data[0];
															}
														});
		}
	}
	var equipment_name = $('#' + equipment + '_name_' + Id).val();
	if(equipment_name == ''){
		$('#' + equipment + '_name_' + Id).css("border-color", "#AAA");
		$('#' + equipment + '_id_' + Id).val("");
		$('#' + equipment + '_quantity_' + Id).val("");
		$('#' + equipment + '_quantity_title_' + Id).val("");
		$('#' + equipment + '_type_span_' + Id).text("");
		return false;
	}
	$.ajax({
		type: 	'POST',
		url:	'../SurgeryEquipment/GetEquipmentInfAjax?set_ajax_view',
		data:	'equipment_name=' + equipment_name ,
		dataType: 'json',
		success: function(data){
			if(data == 1){
				$('#' + equipment + '_name_' + Id).css("border-color", "red");
				$('#error_message').text(_("Medicial instrument (or medicine) does not exist, please input another"));
				$('#' + equipment + '_id_' + Id).val('');
				return;
			}
			$('#' + equipment + '_name_' + Id).css("border-color", "#AAA");
			$('#error_message').text("");
			$('#' + equipment + '_id_' + Id).val(data.surgery_equipment_id);		
			$('#' + equipment + '_type_span_' + Id).text(data.type);
			$('#' + equipment + '_quantity_title_' + Id).val(data.unit);
			$('#' + equipment + '_quantity_' + Id).val(1);		
		} 
	});		
}
	
	
function getTemplateEquipmentInfo(medicine, Id){
	var equipment;
	var medicine_size = $('#templateMedicineTbl tr').length - 2;
	var equipment_size = $('#templateEquipmentTbl tr').length - 2;	
	if(medicine == 'yes'){
		equipment = 'medicine';
		if(Id == medicine_size ){
			var new_index = medicine_size + 1;
			var new_row = " <tr><input id='template_medicine_id_" + new_index + "' name='template_medicine_id_" + new_index + "' type='hidden' value=''>"  
							  + "<td><span id='template_medicine_type_span_"+ new_index +"' name='template_medicine_type_span_"+ new_index +"'></span></td>"
							  + "<td><input type='text' id='template_medicine_name_"+ new_index + "' name='template_medicine_name_"+ new_index + "' value='' onchange=\"javascript:getTemplateEquipmentInfo('yes',"+ new_index +");\"></td>"
							  + "<td><input type='text' id='template_medicine_quantity_"+ new_index + "' name='template_medicine_quantity_"+ new_index + "' size='4' value=''>"
							  + "&nbsp;<input type='text' id='template_medicine_quantity_title_"+ new_index + "' name='template_medicine_quantity_title_"+ new_index + "' size='6' value=''></td>"
							  + "</tr>" ;
			$('#templateMedicineTbl tbody').append(new_row);			
			$('#template_medicine_name_' + new_index).autocomplete('../SurgeryEquipment/searchMedicine?set_ajax_view',
														{
															multiple: false,
															matchContains: true,
															autoFill: true,
															cacheLength: 1,
															formatItem: function(data, i, total) {		
																return data[0];
															}
														});
		}
	}else{
		equipment = 'equipment';
		if(Id == equipment_size ){
			var new_index = equipment_size + 1;
			var new_row = " <tr><input id='template_equipment_id_" + new_index + "' name='template_equipment_id_" + new_index + "' type='hidden' value=''>"
							  + "<td><span id='template_equipment_type_span_"+ new_index +"' name='template_equipment_type_span_"+ new_index +"'></span></td>"  
							  + "<td><input type='text' id='template_equipment_name_"+ new_index + "' name='template_equipment_name_"+ new_index + "' value='' onchange=\"javascript:getTemplateEquipmentInfo('no',"+ new_index +");\"></td>"
							  + "<td><input type='text' id='template_equipment_quantity_"+ new_index + "' name='template_equipment_quantity_"+ new_index + "' size='4' value=''>"
							  + "&nbsp;<input type='text' id='template_equipment_quantity_title_"+ new_index + "' name='template_equipment_quantity_title_"+ new_index + "' size='6' value=''></td>"
							  + "</tr>" ;
			$('#templateEquipmentTbl tbody').append(new_row);			
			
			$('#template_equipment_name_' + new_index).autocomplete('../SurgeryEquipment/searchEquipment?set_ajax_view',
														{
															multiple: false,
															matchContains: true,
															autoFill: true,
															cacheLength: 1,
															formatItem: function(data, i, total) {		
																return data[0];
															}
														});
		}
	}
	var equipment_name = $('#template_' + equipment + '_name_' + Id).val();
	if(equipment_name == ''){
		$('#template_' + equipment + '_name_' + Id).css("border-color", "#AAA");
		$('#template_' + equipment + '_id_' + Id).val("");
		$('#template_' + equipment + '_quantity_' + Id).val("");
		$('#template_' + equipment + '_quantity_title_' + Id).val("");
		$('#template_' + equipment + '_type_span_' + Id).text("");
		return false;
	}
	$.ajax({
		type: 	'POST',
		url:	'../SurgeryEquipment/GetEquipmentInfAjax?set_ajax_view',
		data:	'equipment_name=' + equipment_name ,
		dataType: 'json',
		success: function(data){
			if(data == 1){
				$('#template_' + equipment + '_name_' + Id).css("border-color", "red");				
				$('#template_' + equipment + '_id_' + Id).val('');
				return;
			}
			$('#template_' + equipment + '_name_' + Id).css("border-color", "#AAA");			
			$('#template_' + equipment + '_id_' + Id).val(data.surgery_equipment_id);		
			$('#template_' + equipment + '_type_span_' + Id).text(data.type);
			$('#template_' + equipment + '_quantity_title_' + Id).val(data.unit);
			$('#template_' + equipment + '_quantity_' + Id).val(1);		
		} 
	});		
}

function saveEquipmentTemplate(templateId) {
	var medicine_num = $('#templateMedicineTbl tr').length;
	var equipment_num = $('#templateEquipmentTbl tr').length;
	//check correct medicine
	for(var i = 0; i < medicine_num; i++){
		if($('#template_medicine_name_' + i).val() != '' && $('#template_medicine_id_' + i).val() == ''){
			return false;
		}
	}
	//check correct equipment
 	for(var i = 0; i < equipment_num; i++){
		if($('#template_equipment_name_' + i).val() != '' && $('#template_equipment_id_' + i).val() == ''){
			return false;
		}
	}
	if(!checkQuantityFields(medicine_num, equipment_num, true)){
		return ;
	}
	submitData = $('#template_surgery_equipment_form').serialize();
	$.ajax({
		type:	"GET",
		url:	"../SurgeryEquipment/SaveEquipmentTemplateAjax?set_ajax_view",
		data:	submitData 
				+ "&template_medicine_table_size=" + medicine_num 
				+ "&template_equipment_table_size=" + equipment_num
				+ "&medical_record_id=" + $('#recordId').val() 
				+ "&template_id=" + templateId,
		beforeSend: function() {
			var img = "<img id='updatingTemplate' style='float: left; margin-left: 10px;' src='../images/ajax-loader.gif' />";
			$('.ui-dialog-buttonpane').prepend(img);			
		},		 
		success: function(data){
			$('.ui-dialog-buttonpane img:first-child').remove();
			
		 	var message = "<span id='statusMessageTemplate'" + 
		 		"style='float: left; margin-left: 10px;padding-top:10px; font-style: italic; font-size:1.1em; color: green;'>" +
		 		"Lưu mẫu thành công</span>"
			$('.ui-dialog-buttonpane').prepend(message);
			$('.ui-dialog-buttonpane span:first-child').fadeOut(5000);						
		}
	});
}

function SaveEquipment(type){	
	var medicine_num = $('#medicineTbl tr').length;	
	var equipment_num = $('#equipmentTbl tr').length;
	//check correct medicine
	for(var i = 0; i < medicine_num; i++){
		if($('#medicine_name_' + i).val() != '' && $('#medicine_id_' + i).val() == ''){
			return false;
		}
	}
	//check correct equipment
 	for(var i = 0; i < equipment_num; i++){
		if($('#equipment_name_' + i).val() != '' && $('#equipment_id_' + i).val() == ''){
			return false;
		}
	}
	if(!checkQuantityFields(medicine_num, equipment_num, false)){
		return ;
	}
	submitData = $('#surgery_equipment_form').serialize();
	$.ajax({
		type:	"GET",
		url:	"../SurgeryEquipment/SaveEquipmentAjax?set_ajax_view",
		data:	submitData 
				+ "&medicine_table_size=" + medicine_num 
				+ "&equipment_table_size=" + equipment_num
				+ "&medical_record_id=" + $('#recordId').val(),
		beforeSend: function() {
			$('#equipmentSaveBtn').hide();
			$('#equipmentBarLoading').show();						
		},
		complete: function() {
			$('#equipmentBarLoading').hide();			
			$('#equipmentSaveBtn').show();			
		},		 
		success: function(data){
			$('#save_message').text(data);
		}
	});
}

function getSurgeryEquipment(recordId){
	$.ajax({
		type:	"GET",
		url:	"../SurgeryEquipment/GetCurrentEquipment?set_ajax_view",
		data:	"medical_record_id=" + recordId, 
		success: function(data){
			$('#surgery_equipment_div').html(data).show('slow');			
		}
	});
}
//check values of quantity field 
function checkQuantityFields(medicineTblSize, equipmentTblSize, template){
	var result = true;
	if (template) {
		//check medicine
		for(i = 0; i < medicineTblSize; i++){
			if(!checkFloatingNumber($('#template_medicine_quantity_' + i),_("Invalid quantity"), "templateErrorNumberMessage", '#AAA')){
				result = false;
			}
		}
		//check equipment
		for(i = 0; i < equipmentTblSize; i++){
			if(!checkFloatingNumber($('#template_equipment_quantity_' + i),_("Invalid quantity"), "templateErrorNumberMessage", '#AAA')){
				result = false;
			}
		}
	} else {
		//check medicine
		for(i = 0; i < medicineTblSize; i++){
			if(!checkFloatingNumber($('#medicine_quantity_' + i),_("Invalid quantity"), "errorNumberMessage", '#AAA')){
				result = false;
			}
		}
		//check equipment
		for(i = 0; i < equipmentTblSize; i++){
			if(!checkFloatingNumber($('#equipment_quantity_' + i),_("Invalid quantity"), "errorNumberMessage", '#AAA')){
				result = false;
			}
		}	
	}
	
	return result;
}







